﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Web.backStage.file
{
    public partial class searchChargeLog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                Bind();
        }



        static private string GetConnectionString()
        {
            // To avoid storing the connection string in your code, 
            // you can retrieve it from a configuration file.
            return "Data Source=WEIWANG-PC\\SQLEXPRESS;Initial Catalog=iMovie;Integrated Security=True";
        }

        private void Bind()
        {
            string connectionString = GetConnectionString();
            string sqlString = "";
            sqlString = "select * from Charge_Log";

            SqlConnection myConn = new SqlConnection(connectionString);
            SqlCommand myComd = new SqlCommand(sqlString, myConn);

            SqlDataAdapter myAdapter = new SqlDataAdapter();
            myAdapter.SelectCommand = myComd;

            myConn.Open();
            myAdapter.SelectCommand.ExecuteNonQuery();
            myConn.Close();

            DataSet myDataSet = new DataSet();
            myAdapter.Fill(myDataSet);
            GridView1.DataSource = myDataSet;
            GridView1.DataBind();

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string userid = username.Text.Trim();
            string chargecardid = chargeid.Text.Trim();
          
            string connectionString = GetConnectionString();
            string sqlString = "";
            
            sqlString = "select * from Charge_Log where " ;
            if (userid != "")
            {
                sqlString +=" charge_userId = "+ "'" + userid + "'";
            }
            else
            { sqlString += " 1=1"; }

            if (chargecardid != "")
            {
                sqlString += "and charge_cardId = " + "'" + chargecardid + "'";
            }
            else
            { sqlString += "and 1=1"; }

            SqlConnection myConn = new SqlConnection(connectionString);
            SqlCommand myComd = new SqlCommand(sqlString, myConn);

            SqlDataAdapter myAdapter = new SqlDataAdapter();
            myAdapter.SelectCommand = myComd;

            myConn.Open();
            myAdapter.SelectCommand.ExecuteNonQuery();
            myConn.Close();

            DataSet myDataSet = new DataSet();
            myAdapter.Fill(myDataSet);
            GridView1.DataSource = myDataSet;
            GridView1.DataBind();






        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            String myConnString = GetConnectionString();
            SqlConnection myConn = new SqlConnection(myConnString);
            String sqlString = "";
            sqlString = "delete Charge_Log where charge_id = ";
            sqlString += "'" + id + "' ";
            SqlCommand myCommand = new SqlCommand(sqlString, myConn);
            myConn.Open();
            myCommand.ExecuteNonQuery();
            myConn.Close();




            GridView1.EditIndex = -1;
            Bind();


        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Bind();


        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {


            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string userid = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            string chargecardid = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            string chargeTypeId = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            string chargeDate = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;

            string connectionString = GetConnectionString();
            string sqlString = "";
            sqlString = " update  Charge_Log set ";
            sqlString += " charge_userId = " + "'" + userid + "'" + ",";
            sqlString += " charge_cardId = " + "'" + chargecardid + "'" + ",";
            sqlString += " charge_typeId = " + "'" + chargeTypeId + "'" + ",";
            sqlString += " charge_date= " + "'" + chargeDate + "' ";
            sqlString += " where charge_id = " + "'" + id + "'";

            SqlConnection myConn = new SqlConnection(connectionString);
            SqlCommand myComd = new SqlCommand(sqlString, myConn);
            myConn.Open();
            myComd.ExecuteNonQuery();
            myConn.Close();

            GridView1.EditIndex = -1;

            Bind();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {

            GridView1.EditIndex = -1;
            Bind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
        }
      
    }
}